package demo;

import java.io.FileWriter;
import java.util.*;

/**
 * 7：
 * 创建一个类Employee，内有属性String name, double salary
 * 创建一个主程序类TestEmployee，控制台给出提示信息，让用户依次输入5组数据以创建5个Employee对象
 * 由于不能确定控制台输入的salary是否为double型，所以请创建一个InputDoubleException异常类，并在用户输入错误数据后抛出
 * 请在主程序类中将输入的5组数据以name为key，Employee对象为value存入HashMap中
 * 请遍历HashMap中的键值对，将他们输出到磁盘文件c:\EmployeeInfo.txt
 */
public class TestEmployee_t7 {
    public static void main(String[] args) {
        HashMap<String, Employee_t7> map = new HashMap<>();

        try {
            for (int i = 1; i <= 5; i++) {
                Scanner sc=new Scanner(System.in);
                System.out.println("请输入第"+i+"个人的名字：");
                String name = sc.nextLine();
                System.out.println("请输入第"+i+"个人的薪水：");
                double salary = sc.nextDouble();
                Employee_t7 em = new Employee_t7(name, salary);
                map.put(em.getName(),em);
            }
            System.out.println(map);
            String line = System.getProperty("line.separator");
            StringBuffer str = new StringBuffer();
            FileWriter fw = new FileWriter("c:\\EmployeeInfo.txt", true);
            Set set = map.entrySet();
            Iterator iter = set.iterator();
            while(iter.hasNext()){
                Map.Entry entry = (Map.Entry)iter.next();
                str.append(entry.getKey()+" : "+entry.getValue()).append(line);
            }
            fw.write(str.toString());
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

class Employee_t7{
    private String name;
    private double salary;

    public Employee_t7(String name, double salary) throws Exception {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

class InputDoubleException extends  RuntimeException{

}